package net.ib.mtalk.database.task;

import java.util.UUID;

import net.ib.mntalk.protocol.Session.SendMessageReq;
import net.ib.mtalk.database.DatabaseContract.MessageTable.MessageObjectType;
import net.ib.mtalk.database.DatabaseContract.MessageTable.MessageProperty;
import net.ib.mtalk.database.DatabaseContract.MessageTable.MessageStatus;
import net.ib.mtalk.database.DatabaseContract.MessageTable.SendMessage;
import net.ib.mtalk.database.helper.ChatDatabaseHelper;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.net.Uri;
import android.os.Handler;

public class DBInsertSendMessage extends AsyncClientTask {

	private long mClientViewUuidMsb;
	private long mClientViewUuidLsb;
	private SendMessageReq mSendMessageReq;
	private Uri mUri;
	private MessageProperty mProperty;
	private UUID mClientMsgUuid;
	
	public DBInsertSendMessage(Context context, Handler uiHandler, 
			long clientViewUuidMsb, long clientViewUuidLsb, SendMessageReq sendMessageReq, 
			MessageProperty property, Uri uri, UUID clientMsgUuid){
		super(context, uiHandler);
		MTalkLogWriter.d("DBInsertSendMessage");
		this.mClientViewUuidMsb = clientViewUuidMsb;
		this.mClientViewUuidLsb = clientViewUuidLsb;
		this.mSendMessageReq = sendMessageReq;
		this.mClientMsgUuid = clientMsgUuid;
		this.mUri = uri;
		this.mProperty = property;
	}
	
	public DBInsertSendMessage(Context context, Handler uiHandler, 
			UUID clientViewUuid, SendMessageReq sendMessageReq, 
			MessageProperty property, Uri uri, UUID clientMsgUuid){
		this(context, uiHandler, clientViewUuid.getMostSignificantBits(), clientViewUuid.getLeastSignificantBits(), 
				sendMessageReq,  property, uri, clientMsgUuid);
	}
	
	public DBInsertSendMessage(Context context, Handler uiHandler, 
			long clientViewUuidMsb, long clientViewUuidLsb, SendMessageReq sendMessageReq, 
			 UUID clientMsgUuid) {
		this(context, uiHandler, clientViewUuidMsb, clientViewUuidLsb, 
				sendMessageReq,  null, null, clientMsgUuid);
	}
	
	public DBInsertSendMessage(Context context, Handler uiHandler, 
			UUID clientViewUuid, SendMessageReq sendMessageReq, 
			 UUID clientMsgUuid) {
		this(context, uiHandler, clientViewUuid, 
				sendMessageReq,  null, null, clientMsgUuid);
	}
	
	@Override
	protected void onPostExecute(int resultCode, Object sentObject,
			Object receivedObject) {
		MTalkLogWriter.d("onPostExecute");
		ChatDatabaseHelper.insertSendMessage(mContext,
				new UUID(mClientViewUuidMsb, mClientViewUuidLsb),
				mSendMessageReq, 
				MessageObjectType.USER, 
				MessageStatus.SENDING, 
				SendMessage.YES,
				mProperty,
				mUri,
				mClientMsgUuid);
	}

}
